// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import { commands, ExtensionContext, extensions, ViewColumn, window, workspace } from 'vscode';
import { Book } from './bookUtil';
import { ConfigUtil } from './lib/config';
import { NodeDependenciesProvider } from './NodeDependenciesProvider';
import { registerRequestGroupProvider, RequestGroupProvider } from './RequestGroupProvider';
import * as fs from 'fs';

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: ExtensionContext) {

	// Use the console to output diagnostic information (console.log) and errors (console.error)
	// This line of code will only be executed once when your extension is activated
	console.log('Congratulations, your extension "fish-book" is now active!');

	let books = new Book(context.extensionPath);
	window.setStatusBarMessage("fish book init");

	// "activationEvents": [
	// 	"onCommand:vscode-myfrist.helloWorld"
	// ],

	// The command has been defined in the package.json file
	// Now provide the implementation of the command with registerCommand
	// The commandId parameter must match the command field in package.json

	// 老板键
	let displayCode = commands.registerCommand('fishbook.displayCode', () => {

		let lauageArrList = [
			'Java - System.out.println("Hello World");',
			'C++ - cout << "Hello, world!" << endl;',
			'C - printf("Hello, World!");',
			'Python - print("Hello, World!")',
			'PHP - echo "Hello World!";',
			'Ruby - puts "Hello World!";',
			'Perl - print "Hello, World!";',
			'Lua - print("Hello World!")',
			'Scala - println("Hello, world!")',
			'Golang - fmt.Println("Hello, World!")'
		];

		var index = Math.floor((Math.random() * lauageArrList.length));
		window.setStatusBarMessage(lauageArrList[index]);
	});

	// 下一页
	let getNextPage = commands.registerCommand('fishbook.getNextPage', () => {
		window.setStatusBarMessage(books.getNextPage());
	});

	// 上一页
	let getPreviousPage = commands.registerCommand('fishbook.getPreviousPage', () => {
		window.setStatusBarMessage(books.getPreviousPage());
	});

	// 跳转某个页面
	let getJumpingPage = commands.registerCommand('fishbook.getJumpingPage', () => {
		window.showInputBox({
			title: "Jump page",
			placeHolder: "Current pageNumber is " + ConfigUtil.getCurPageNumber(),
			validateInput: (str) => {
				let val = parseInt(str);
				if (!isNaN(val)) {
					return;
				}

				return 'Int require!';
			}
		}).then((val) => {
			if (!val) {
				return;
			}
			window.setStatusBarMessage(books.getJumpingPage(parseInt(val!)));
		});
	});

	context.subscriptions.push(displayCode);
	context.subscriptions.push(getNextPage);
	context.subscriptions.push(getPreviousPage);
	context.subscriptions.push(getJumpingPage);

	// 

	// The command has been defined in the package.json file
	// Now provide the implementation of the command with registerCommand
	// The commandId parameter must match the command field in package.json
	let disposable = commands.registerCommand('fishbook.bookStart', () => {
		// The code you place here will be executed every time your command is executed
		// Display a message box to the user
	});

	context.subscriptions.push(disposable);

	context.subscriptions.push(commands.registerCommand("fishbook.bookInfo", () => {
		window.showInformationMessage(books.getCurBookInfo());
	}));

	// Node dependencies
	const rootPath = (workspace.workspaceFolders?.length ?? 0) > 0 ? workspace.workspaceFolders![0].uri.fsPath : undefined;
	let nodeDependenciesProvider = new NodeDependenciesProvider(rootPath!);
	if (rootPath) {
		window.registerTreeDataProvider('nodeDependencies', nodeDependenciesProvider);
	}
	commands.registerCommand('nodeDependencies.refreshEntry', () =>
		nodeDependenciesProvider.refresh()
	);

	registerRequestGroupProvider(context, books);
}

// this method is called when your extension is deactivated
export function deactivate() { }
